<?php


namespace App\Tools;


use App\Models\RaffleDetail;
use App\Models\WxUser;
use Illuminate\Support\Facades\DB;

class RaffledetailsTools
{
        /**
         * @Notes:
         * @Author: 遇见
         * @Date: 2022/12/1
         * @Time: 18:33
         * @param $data
         * @param $rid
         * @return array
         * 处理用户抽奖信息
         */
        public function raff($data,$rid){
            DB::beginTransaction();
            try {
                RaffleDetail::create($data[0]);
                $user=  WxUser::where('id',$data[0]['uid'])->first();
                WxUser::where('id',$data[0]['uid'])->update([
                   'user_integral'=>$user['user_integral']+$data[0]['prize']
                ]);
                DB::commit();
            }catch (\Exception $exception){
                DB::rollBack();
                return JsonTools::JsonError([],$exception->getCode(),$exception->getMessage());
            };
            $wxUser=WxUser::where('id',$data[0]['uid'])->first();
            if($wxUser['user_integral']>=100){
                $data = new \App\Tools\DxbObserver();
                $data->attach(new \App\Tools\DxbTools());//添加观察者
                $data->notify($wxUser['user_phone']);//执行观察者操作
                $data->detach(new \App\Tools\DxbTools());//删除观察者
            }
            return JsonTools::JsonSuccess($rid);
        }
}
